package com.gm.wholesale.module.good.view;

import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.gm.commonlib.filters.RegexConstant;
import com.gm.commonlib.filters.RegexResultFilter;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.choiceview.CustomItemChoosenEntity;
import com.gm.commonlib.views.choiceview.CustomItemChosenButton;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.good.GoodType;
import com.gm.wholesale.module.good.bean.CreateGoodReq;
import com.gm.wholesale.module.good.bean.GoodDetailBean;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.ui.goodManger.CreateGoodFragment;
import com.gm.wholesale.module.kind.bean.KindBean;
import com.gm.wholesale.module.unit.bean.UnitBean;

import java.util.ArrayList;
import java.util.List;

public class UnInventoryGoodView extends LinearLayout implements View.OnClickListener {


    private CustomItemChosenButton choiceKindTv;
    private ImageView addKindImg;
    private EditText prictEd1;
    private CustomItemChosenButton choiceUnit1;
    private EditText prictEd2;
    private CustomItemChosenButton choiceUnit2;
    private EditText prictEd3;
    private CustomItemChosenButton choiceUnit3;
    private EditText prictEd4;
    private CustomItemChosenButton choiceUnit4;


    private UnitBean unitBean1, unitBean2, unitBean3, unitBean4;

    public UnInventoryGoodView(Context context) {
        super(context);
        initView(context);
    }

    public UnInventoryGoodView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private CreateGoodFragment fragment;
    private Context mContext;

    public void setCreateGoodFragment(CreateGoodFragment fragment) {
        this.fragment = fragment;
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_create_uinventory_good, this);
        choiceKindTv = findViewById(R.id.choiceKindTv);
        addKindImg = findViewById(R.id.addKindImg);
        prictEd1 = findViewById(R.id.prictEd1);
        choiceUnit1 = findViewById(R.id.choiceUnit1);
        prictEd2 = findViewById(R.id.prictEd2);
        choiceUnit2 = findViewById(R.id.choiceUnit2);
        prictEd3 = findViewById(R.id.prictEd3);
        choiceUnit3 = findViewById(R.id.choiceUnit3);
        prictEd4 = findViewById(R.id.prictEd4);
        choiceUnit4 = findViewById(R.id.choiceUnit4);

        choiceKindTv.setOnClickListener(this);
        addKindImg.setOnClickListener(this);
        choiceUnit1.setOnClickListener(this);
        choiceUnit2.setOnClickListener(this);
        choiceUnit3.setOnClickListener(this);
        choiceUnit4.setOnClickListener(this);


        setInputFilter(prictEd1);
        setInputFilter(prictEd2);
        setInputFilter(prictEd3);
        setInputFilter(prictEd4);

        choiceKindTv.setOnCustomItemClickListener(new CustomItemChosenButton.IOnCustomItemClickListener() {
            @Override
            public void onItemChoosenButtonClick(CustomItemChosenButton button, int itemText, CustomItemChoosenEntity entity) {
                fragment.mChoiceKind = (KindBean) entity;
            }
        });
        choiceUnit1.setOnCustomItemClickListener(new CustomItemChosenButton.IOnCustomItemClickListener() {
            @Override
            public void onItemChoosenButtonClick(CustomItemChosenButton button, int itemText, CustomItemChoosenEntity entity) {
                unitBean1 = (UnitBean) entity;
                if (unitBean1.getId()<=0){
                    choiceUnit1.setText("");
                    unitBean1=null;
                }
            }
        });

        choiceUnit2.setOnCustomItemClickListener(new CustomItemChosenButton.IOnCustomItemClickListener() {
            @Override
            public void onItemChoosenButtonClick(CustomItemChosenButton button, int itemText, CustomItemChoosenEntity entity) {
                unitBean2 = (UnitBean) entity;
                if (unitBean2.getId()<=0){
                    choiceUnit2.setText("");
                    unitBean2=null;
                }
            }
        });
        choiceUnit3.setOnCustomItemClickListener(new CustomItemChosenButton.IOnCustomItemClickListener() {
            @Override
            public void onItemChoosenButtonClick(CustomItemChosenButton button, int itemText, CustomItemChoosenEntity entity) {
                unitBean3 = (UnitBean) entity;
                if (unitBean3.getId()<=0){
                    choiceUnit3.setText("");
                    unitBean3=null;
                }
            }
        });
        choiceUnit4.setOnCustomItemClickListener(new CustomItemChosenButton.IOnCustomItemClickListener() {
            @Override
            public void onItemChoosenButtonClick(CustomItemChosenButton button, int itemText, CustomItemChoosenEntity entity) {
                unitBean4 = (UnitBean) entity;
                if (unitBean4.getId()<=0){
                    choiceUnit4.setText("");
                    unitBean4=null;
                }
            }
        });


    }

    public void showView(GoodDetailBean goodDetailBean){
        if (goodDetailBean.getCategory()!=null){
            choiceKindTv.setText(goodDetailBean.getCategory().getPrimaryName());
        }
      List<GoodListBean.UnitsBean> unitsBeans=  goodDetailBean.getUnits();
        if (unitsBeans==null)return;
        int pos=0;
        for (GoodListBean.UnitsBean unitsBean:unitsBeans){
             setPriceView(pos,unitsBean);
             pos++;
        }
    }

    private void setPriceView(int pos, GoodListBean.UnitsBean unitsBean){
            switch (pos){
                case 0:
                    unitBean1=unitsBean.getUnitBean();
                    prictEd1.setText(NumberUtils.subZeroAndDot(unitsBean.getPrice()));
                    choiceUnit1.setText(unitsBean.getName());
                    break;
                case 1:
                    unitBean2=unitsBean.getUnitBean();
                    prictEd2.setText(NumberUtils.subZeroAndDot(unitsBean.getPrice()));
                    choiceUnit2.setText(unitsBean.getName());
                    break;
                case 2:
                    unitBean3=unitsBean.getUnitBean();
                    prictEd3.setText(NumberUtils.subZeroAndDot(unitsBean.getPrice()));
                    choiceUnit3.setText(unitsBean.getName());
                    break;
                case 3:
                    unitBean4=unitsBean.getUnitBean();
                    prictEd4.setText(NumberUtils.subZeroAndDot(unitsBean.getPrice()));
                    choiceUnit4.setText(unitsBean.getName());
                    break;

            }
    }


    private void choiceUnit(CustomItemChosenButton chosenButton) {

        fragment.choiceUnit(chosenButton,false);
    }

    private void setInputFilter(EditText editText) {
        editText.setFilters(new InputFilter[]{new RegexResultFilter(RegexConstant.MAX_MONEY_VALUE_92)});
    }

    public String checkData(String goodName) {
        if (TextUtils.isEmpty(goodName)) {
            return "请输入商品名称";
        }
        if (!TextUtils.isEmpty(prictEd1.getText().toString()) && unitBean1==null){
           return  "价签1 单位不能为空";
        }

        if (TextUtils.isEmpty(prictEd1.getText().toString()) && unitBean1!=null){
            return  "价签1 单价不能为空";
        }
        if ("0".equals(prictEd1.getText().toString())){
            return  "价签1 价格不能为0";
        }


        if (!TextUtils.isEmpty(prictEd2.getText().toString()) && unitBean2==null){
            return  "价签2 单位不能为空";
        }
        if (TextUtils.isEmpty(prictEd2.getText().toString()) && unitBean2!=null){
            return  "价签2 单价不能为空";
        }
        if ("0".equals(prictEd2.getText().toString())){
            return  "价签2 价格不能为0";
        }




        if (!TextUtils.isEmpty(prictEd3.getText().toString()) && unitBean3==null){
            return  "价签3 单位不能为空";
        }
        if (TextUtils.isEmpty(prictEd3.getText().toString()) && unitBean3!=null){
            return  "价签3 单价不能为空";
        }
        if ("0".equals(prictEd3.getText().toString())){
            return  "价签3 价格不能为0";
        }



        if (!TextUtils.isEmpty(prictEd4.getText().toString()) && unitBean4==null){
            return  "价签4 单位不能为空";
        }
        if (TextUtils.isEmpty(prictEd4.getText().toString()) && unitBean4!=null){
            return  "价签4 单价不能为空";
        }
        if ("0".equals(prictEd4.getText().toString())){
            return  "价签4 价格不能为0";
        }

        return "";
    }


    public CreateGoodReq getCreateGoodReq(String goodName) {
        CreateGoodReq createGoodReq = new CreateGoodReq();
        if (fragment.mChoiceKind != null) {
            createGoodReq.setCategoryId(fragment.mChoiceKind.getId());
        }
        createGoodReq.setName(goodName);
        //2: 非库存商品，3: 库存商品
        createGoodReq.setType(GoodType.UN_INVENTORY_GOOD);
        List<CreateGoodReq.UnitsBean> unitBeans = new ArrayList<>();

        //todo
        if (getUnitsBean(unitBean1, prictEd1) != null) {
            unitBeans.add(getUnitsBean(unitBean1, prictEd1));
        }
        if (getUnitsBean(unitBean2, prictEd2) != null) {
            unitBeans.add(getUnitsBean(unitBean2, prictEd2));
        }
        if (getUnitsBean(unitBean3, prictEd3) != null) {
            unitBeans.add(getUnitsBean(unitBean3, prictEd3));
        }
        if (getUnitsBean(unitBean4, prictEd4) != null) {
            unitBeans.add(getUnitsBean(unitBean4, prictEd4));
        }
        createGoodReq.setUnits(unitBeans);
        return createGoodReq;

    }


    private CreateGoodReq.UnitsBean getUnitsBean(UnitBean data, EditText priceEd) {
        if (data != null) {
            CreateGoodReq.UnitsBean unitsBean = new CreateGoodReq.UnitsBean();
            unitsBean.setId(data.getId());
            unitsBean.setType(2);
            unitsBean.setRatio(data.getProportionNum());
            String price = priceEd.getText().toString();
            if (!TextUtils.isEmpty(price)) {
                unitsBean.setPrice(price);
            }
            return unitsBean;
        }
        return null;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.addKindImg) {
            fragment.createKind(choiceKindTv);
        }
       else if (id == R.id.choiceKindTv) {
            fragment.showKind(choiceKindTv);
        } else if (id == R.id.choiceUnit1) {
            choiceUnit(choiceUnit1);
        } else if (id == R.id.choiceUnit2) {
            choiceUnit(choiceUnit2);
        } else if (id == R.id.choiceUnit3) {
            choiceUnit(choiceUnit3);
        } else if (id == R.id.choiceUnit4) {
            choiceUnit(choiceUnit4);
        }
    }


}
